/*
 * ExportWindow.java
 *
 * Created on __DATE__, __TIME__
 */

package gui;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;

import core.Core;

/**
 * 
 * @author __USER__
 */
public class ExportWindow extends javax.swing.JDialog {

	/** Creates new form ExportWindow */
	public ExportWindow(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		jRadioButton1.setSelected(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		jRadioButton1 = new javax.swing.JRadioButton();
		jRadioButton2 = new javax.swing.JRadioButton();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jPanel1.setBackground(new java.awt.Color(141, 178, 227));

		jLabel1.setIcon(new javax.swing.ImageIcon("icones/cd.png")); // NOI18N

		jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jPanel2.setOpaque(false);

		jRadioButton1.setBackground(new java.awt.Color(141, 178, 227));
		buttonGroup1.add(jRadioButton1);
		jRadioButton1.setText("Make a backup");

		jRadioButton2.setBackground(new java.awt.Color(141, 178, 227));
		buttonGroup1.add(jRadioButton2);
		jRadioButton2.setText("Open a backup");

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addGap(25, 25, 25)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jRadioButton2)
														.addComponent(
																jRadioButton1))
										.addContainerGap(270, Short.MAX_VALUE)));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel2Layout.createSequentialGroup().addGap(22, 22, 22)
						.addComponent(jRadioButton1).addGap(18, 18, 18)
						.addComponent(jRadioButton2)
						.addContainerGap(26, Short.MAX_VALUE)));

		jButton1.setText("Cancel");
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callCancel(evt);
			}
		});

		jButton2.setText("Next");
		jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callNext(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						jPanel1Layout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addGap(32, 32, 32)
								.addComponent(jPanel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(18, Short.MAX_VALUE))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						jPanel1Layout.createSequentialGroup()
								.addContainerGap(384, Short.MAX_VALUE)
								.addComponent(jButton1).addGap(29, 29, 29)
								.addComponent(jButton2).addGap(27, 27, 27)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jPanel2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel1))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												26, Short.MAX_VALUE)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton2)
														.addComponent(jButton1))
										.addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	private void callNext(java.awt.event.MouseEvent evt) {
		JFileChooser jfc = new JFileChooser(".");
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		jfc.addChoosableFileFilter(new FileFilter() {
			
			final private String description = new String("xml file");
			public boolean accept(File file){
			      if(file.isDirectory()) { 
			         return true; 
			      } 
			      String nomFichier = file.getName().toLowerCase(); 
			      return nomFichier.endsWith(".xml");
			   }

			@Override
			public String getDescription() {
				return description;
			}

		});
		if (jRadioButton1.isSelected()) {
			if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
				File file = jfc.getSelectedFile();

				if (file.exists()) {
					DialogWindow dw = new DialogWindow(
							null,
							true,
							"Warning",
							"Do you really want to ovewrite the existing file?",
							2, false);
					dw.setVisible(true);
					if (dw.getReturnStatus() == DialogWindow.RET_OK) {
						Core.get().exportXml(file.getPath());
						this.dispose();
					}
				} else {
					Core.get().exportXml(file.getPath());
					this.dispose();
				}
			}
		} else if (jRadioButton2.isSelected()) {
			if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				File file = jfc.getSelectedFile();

				if (file.exists()) {
					DialogWindow dw = new DialogWindow(
							null,
							true,
							"Warning",
							"Do you really want to import this file? All the current datas will be lost",
							2, false);
					dw.setVisible(true);
					if (dw.getReturnStatus() == DialogWindow.RET_OK) {
						// Core.get().importXml(file.getPath());
						PrincipalWindow frame = (PrincipalWindow) getParent();
						frame.refreshTable();
						this.dispose();
					}

					System.out.println("exists");
				} else {
					System.out.println("doesn't exist");
					DialogWindow dw = new DialogWindow(null, true, "Error",
							"The selected file doesn't exist", 1, true);
					dw.setVisible(true);
				}
			}
		}
	}

	private void callCancel(java.awt.event.MouseEvent evt) {
		this.dispose();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				ExportWindow dialog = new ExportWindow(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JRadioButton jRadioButton1;
	private javax.swing.JRadioButton jRadioButton2;
	// End of variables declaration//GEN-END:variables

}